package com.xmtlxx.common.wxap.util;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class DomainUtil {

	/**
	 * 获得网站域名
	 *
	 * @param request
	 * @return 网站的域名（比如：localhost:8080/ibank-web），不包含http://
	 */
	public static String getDomainName(HttpServletRequest request) {
		String domainName = "";

		//String ctx = request.getContextPath();
		String URI = request.getRequestURI();
		String URL = request.getRequestURL().toString();

		//domainName = URL.substring(0, URL.indexOf(URI)) + ctx;
		domainName = URL.substring(0, URL.indexOf(URI));

		domainName = domainName.replaceFirst("http://", "");

		return domainName;
	}

	/**
	 * 获取当前服务器的IP地址
	 * @return
	 * @throws UnknownHostException
	 */
	public static String getIpAddress() {
		InetAddress address;
		try {
			address = InetAddress.getLocalHost();
			return address.getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return "";
	}

}
